//----------------------------//
//     PowerfulShotguns       //
//           by               //
//        bdking00            //
//----------------------------//
using GTA;
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using GTA.Native;
using GTA.Math;

public class FirstGTAVMod : Script
{
    bool on = false;

    int power;

    int radius = 250;

    private ScriptSettings config;

    public FirstGTAVMod()
    {
        config = ScriptSettings.Load("scripts\\PowerfulShotguns.ini");

        Tick += OnTick;
        KeyDown += OnKeyDown;
        KeyUp += OnKeyUp;

        Interval = 10;

        power = config.GetValue<int>("Settings", "Power", 100);
    }
    void OnTick(object sender, EventArgs e)
    {
        Ped player = Game.Player.Character;

        Player pl = Game.Player;

        Vehicle[] veh = World.GetNearbyVehicles(player, radius);

        Ped[] ped = World.GetNearbyPeds(player, radius);

        if (on)
        {
            if (player.Weapons.Current.Hash.Equals(WeaponHash.AssaultShotgun) || player.Weapons.Current.Hash.Equals(WeaponHash.BullpupShotgun) || player.Weapons.Current.Hash.Equals(WeaponHash.HeavyShotgun)
                || player.Weapons.Current.Hash.Equals(WeaponHash.PumpShotgun) || player.Weapons.Current.Hash.Equals(WeaponHash.SawnOffShotgun))
            {
                if (player.IsShooting)
                {
                    for (int i = 0; i < veh.Length; i++)
                    {
                        if (pl.IsTargetting(veh[i]))
                        {
                            shootVeh(player, veh[i]);
                        }
                    }
                    DeleteVEHArray(veh);
                    for (int i = 0; i < ped.Length; i++)
                    {
                        if (pl.IsTargetting(ped[i]))
                        {
                            if (ped[i].IsInVehicle())
                            {
                                ped[i].CurrentVehicle.ApplyForce(player.ForwardVector * power);
                            }
                            else if (pl.IsTargetting(ped[i]))
                            {
                                shootPed(player, ped[i]);
                            }
                        }
                    }
                    DeletePEDArray(ped);
                }
            }
            if (!player.Weapons.Current.Hash.Equals(WeaponHash.AssaultShotgun) || !player.Weapons.Current.Hash.Equals(WeaponHash.BullpupShotgun) || !player.Weapons.Current.Hash.Equals(WeaponHash.HeavyShotgun)
                || !player.Weapons.Current.Hash.Equals(WeaponHash.PumpShotgun) || !player.Weapons.Current.Hash.Equals(WeaponHash.SawnOffShotgun))
            {

            }
        }
    }

    void OnKeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.PageUp)
        {
            power += 1;
            GTA.UI.ShowSubtitle("Power is now " + power.ToString());
        }
        if (e.KeyCode == Keys.PageDown)
        {
            power -= 1;
            GTA.UI.ShowSubtitle("Power is now " + power.ToString());
        }
    }

    void OnKeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.End)
        {
            on = !on;
            if (on)
            {
                GTA.UI.ShowSubtitle("Powerful Shotguns enabled!");
            }
            else
            {
                GTA.UI.ShowSubtitle("Powerful Shotguns disabled");
            }
        }
    }
    void shootPed(Ped player, Ped ped)
    {
        Function.Call(Hash.SET_PED_TO_RAGDOLL, ped, 6000, 6000, 0, 1, 1, 1);
        ped.ApplyForce(player.ForwardVector * power);

    }
    void shootVeh(Ped player, Vehicle veh)
    {
        float vehPower = power - 5;
        veh.ApplyForce(player.ForwardVector * power);
    }
    void DeletePEDArray(Ped[] pedObj)
    {
        for (int i = 0; i < pedObj.Length; i++)
        {
            pedObj[i] = null;
        }
        pedObj = null;
    }

    void DeleteVEHArray(Vehicle[] vObj)
    {
        for (int i = 0; i < vObj.Length; i++)
        {
            vObj[i] = null;
        }
        vObj = null;
    }
}